package com.yygo.webapp.controller.money;

import com.yygo.Constants;
import com.yygo.model.LoanRequest;
import com.yygo.model.MoneyTransferRecord;
import com.yygo.model.Prepayment;
import com.yygo.model.User;
import com.yygo.service.*;
import com.yygo.util.DateUtil;
import com.yygo.webapp.helper.ExtendedPaginatedList;
import com.yygo.webapp.helper.PaginateListFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by tiansha on 2015/11/7.
 */
@Controller
public class MoneyTransferController {

    @Autowired
    private MoneyTransferManager moneyTransferManager;

    @Autowired
    private LoanRequestManager loanRequestManager;

    @Autowired
    private MoneyService moneyService;

    private PaginateListFactory paginateListFactory;
    @Autowired
    public void setPaginateListFactory(PaginateListFactory paginateListFactory) {
        this.paginateListFactory = paginateListFactory;
    }

    @Autowired
    private UserManager userManager;

    @Autowired
    private PrepaymentManager prepaymentManager;

    @RequestMapping(value = "/loanedRequests", method = RequestMethod.GET)
    public Model handleRequest(@RequestParam(required = false, value = "date") Date date,
                               @RequestParam(required = false, value = "number") String number, @RequestParam(required = false, value = "name") String name,
                               HttpServletRequest request, Principal principal)
            throws Exception {
        ExtendedPaginatedList paginatedList = paginateListFactory
                .getPaginatedListFromRequest(request);

        Model model = new ExtendedModelMap();

        if (date == null) {
            date = DateUtil.getDateBefore(10);
        }
        User loginUser = userManager.getUserByUsername(principal.getName());
        List<LoanRequest> list = loanRequestManager.getLoanedToTransferRequests(loginUser, date, number, name, paginatedList);
        model.addAttribute("loanRequestList", paginatedList);

        model.addAttribute("loginUser", loginUser);

        return model;
    }

    @RequestMapping(value="/batchApproveTransfer", method = RequestMethod.GET)
    public String batchApproveTransfer(@RequestParam(required = true, value = "selectedIds") Long[] selectedIds, Principal principal) {
        for (Long id : selectedIds) {
            moneyService.approveTransfer(id, principal.getName());
        }
        return "redirect:/loanedRequests";
    }


    @RequestMapping(value = "/transferredMoneyTransferRecords", method = RequestMethod.GET)
    public Model transferredMoneyTransferRecords(Principal principal) {
        User loginUser = userManager.getUserByUsername(principal.getName());
        Model model = new ExtendedModelMap();
        List<MoneyTransferRecord> moneyTransferRecords = moneyTransferManager.getTransferredMoneyTransferRecords(loginUser);
        model.addAttribute("moneyTransferRecordList", moneyTransferRecords);
        return model;
    }

    @RequestMapping(value = "/pendingMoneyTransferRecords", method = RequestMethod.GET)
    public Model pendingMoneyTransferRecords(Principal principal) {
        User loginUser = userManager.getUserByUsername(principal.getName());
        Model model = new ExtendedModelMap();
        List<MoneyTransferRecord> moneyTransferRecords = moneyTransferManager.getPendingMoneyTransferRecords(loginUser);
        model.addAttribute("moneyTransferRecordList", moneyTransferRecords);
        return model;
    }

    @RequestMapping(value="/batchTransferMoney", method = RequestMethod.GET)
    public String batchTransfer(@RequestParam(required = true, value = "selectedIds") Long[] selectedIds, Principal principal) {
        for (Long id : selectedIds) {
            moneyService.transferMoney(id, principal.getName());
        }
        return "redirect:/pendingMoneyTransferRecords";
    }


}
